#ifndef DODO_DODO_COMMAND_H
#define DODO_DODO_COMMAND_H

#ifdef __cplusplus
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
namespace claydodo
{
    std::string Command ( const std::string & cmd ) throw ( std::string )
    {
        FILE * respipe = popen ( cmd.c_str(), "r" );
        if ( ! respipe )
        {
            throw ( std::string ( "Command Failed: cannot popen" ) );
        }
        char buf[1024];
        char *res;
        if ( ( res = fgets ( buf, 1024, respipe ) ) != NULL )
        {
            int len = strlen ( buf );
            if ( len > 0 && buf[len-1] == '\n' )
                buf[len-1] = '\0';
        }
        else
        {
            strcpy ( buf, "" );
        }
        pclose ( respipe );
        return std::string ( buf );
    }
}
#else
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char buf[1024];
char * Command ( const char * cmd )
{
    FILE * respipe = popen ( cmd, "r" );
    if ( ! respipe )
    {
        strcpy ( buf, "Command Failed: cannot popen" );
        return buf;
    }
    char *res;
    if ( ( res = fgets ( buf, 1024, respipe ) ) != NULL )
    {
        int len = strlen ( buf );
        if ( len > 0 && buf[len-1] == '\n' )
            buf[len-1] = '\0';
    }
    else
    {
        strcpy ( buf, "" );
    }
    pclose ( respipe );
    return buf;
}
#endif

#endif

